package com.team.mr.mrboot.service.impl;

import com.team.mr.mrboot.entity.Order;
import com.team.mr.mrboot.dao.OrderDao;
import com.team.mr.mrboot.result.ResponseCode;
import com.team.mr.mrboot.result.ResponseData;
import com.team.mr.mrboot.service.OrderService;
import com.team.mr.mrboot.util.SmsUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Order)表服务实现类
 *
 * @author makejava
 * @since 2021-06-01 09:29:27
 */
@Service("orderService")
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderDao orderDao;

    /**
     * 通过ID查询单条数据
     *
     * @param oId 主键
     * @return 实例对象
     */
    @Override
    public Order queryById(Integer oId) {
        return this.orderDao.queryById1(oId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Order> queryAllByLimit(int offset, int limit) {
        return this.orderDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param order 实例对象
     * @return 实例对象
     */

    @Transactional
    @Override
    public ResponseData insert(Order order) {
        String code=SmsUtil.sendSms(order.getTel());
        if (code == null){
            return new ResponseData("00201","预约失败，请核对手机信息！",null);
        }
        Date date=new Date();
        order.setSubtime(date);
        order.setStatus(0);
        order.setCode(code);
        int i = orderDao.insert(order);
        if (i > 0){
            return new ResponseData(ResponseCode.SUCCESS,null);
        }
        else{
            return new ResponseData(ResponseCode.FAILED,null);
        }
    }

    /**
     * 修改数据
     *
     * @param order 实例对象
     * @return 实例对象
     */
    @Override
    public Order update(Order order) {
        this.orderDao.update(order);
        return this.queryById(order.getoId());
    }

    /**
     * 通过主键删除数据
     *
     * @param oId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer oId) {
        return this.orderDao.deleteById(oId) > 0;
    }
}
